import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.bind.annotation.XmlRootElement;

import codeg.CodeGeneration;
import codeg.CodeGenerationContext;
import codeg.Codeg;
import codeg.CodegException;


enum OPTION {
    FILE, TEXT;
}


enum OUTPUT {
    STRING, CHARARRAY;
}


@XmlRootElement
public class TextCodeg extends Codeg {
    private OPTION option = OPTION.TEXT;
    private String text = null;
    private File file = null;
    private OUTPUT output = OUTPUT.STRING;

    public TextCodeg() {
        super();
    }

    public OPTION getOption() {
        return option;
    }

    public void setOption(OPTION option) {
        this.option = option;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public OUTPUT getOutput() {
        return output;
    }

    public void setOutput(OUTPUT output) {
        this.output = output;
    }

    public static String readTextFromFile(File file) {
        if (file == null) return "";
        else if (!file.canRead()) return "Error: File cannot be read";

        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(file));
        }
        catch (FileNotFoundException e) {
            return "Error: File may not exist";
        }
        StringBuilder text = new StringBuilder();
        try {
            String line = br.readLine();
            while (line != null) {
                text.append(line + System.getProperty("line.separator"));
                line = br.readLine();
            }
        }
        catch (IOException e) {
            return "Error reading file";
        }

        return text.toString();
    }
    
    @Override
    public CodeGeneration getCode(CodeGenerationContext context)
        throws CodegException {
        CodeGeneration code = new CodeGeneration(this);

        /* This codeg only generates Java code currently */
        if (context.getLanguage() != CodeGenerationContext.LANGUAGE.JAVA)
            return code;
        
        OUTPUT output = getOutput();
        OPTION option = getOption();
        
        String text = "";
        if (option == OPTION.TEXT)
            text = getText();
        else if (option == OPTION.FILE)
            text = readTextFromFile(getFile());
        if (text == null) text = "";
        
        if (output == OUTPUT.STRING) {
            code.setLocalCode("\"" + 
                text.replace(System.getProperty("line.separator"), "\" +\n\"") 
                + "\"");
        }
        else if (getOutput() == OUTPUT.CHARARRAY) {
            char[] charArray = text.toCharArray();
            StringBuilder sb = new StringBuilder();
            for (char c : charArray) {
                sb.append('\'');
                sb.append(c);
                sb.append('\'');
                sb.append(", ");
            }
            sb.delete(sb.length() - 2, sb.length());
            
            code.setLocalCode("{ " + sb.toString() + " }");
        }
        
        return code;
    }
}